package com.scqkxx.gamebox.activity.group;

import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.FunctionCallback;
import com.jyn.vcview.VerificationCodeView;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.databinding.ActivityJoinGroupBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

public class GroupJoinActivity extends BaseActivity<ActivityJoinGroupBinding> {

    private String code;

    @Override
    public int setLayoutId() {
        return R.layout.activity_join_group;
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener(mTitleType -> finish());

        binding.inputCode.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
            @Override
            public void onTextChange(View view, String content) {

            }

            @Override
            public void onComplete(View view, String content) {
                code = content;
            }
        });

        binding.btCommit.setOnClickListener(view -> {
            Map<String, Object> map = new HashMap<>();
            map.put("checkCode", code);
            AVCloud.callFunctionInBackground("joinGroup", map, new FunctionCallback<Map<String, Object>>() {
                @Override
                public void done(Map<String, Object> result, AVException e) {
                    Log.i("joinGroup", result + "");//{msg=邀请码创建成功, data={checkCode=7726}, error_code=0}
                    if (e == null) {
                        if ("0".equals(result.get("error_code") + "")) {
                            ToastUtils.showShortToast(GroupJoinActivity.this,"加入成功");
                            finish();
                        }else {
                            ToastUtils.showShortToast(GroupJoinActivity.this,result.get("msg") + "");
                        }
                    }
                }
            });
        });
    }
}
